﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace Noea.HTTP
{
    public class HTTPSocket : IHTTP
    {
        private Socket socket;
        private Uri uri;
        private string address = "127.0.0.1";
        private int port = 80;
        private int timeout = 1000;  // mililiseconds to wait when downloading pages
        //private NotifyLog notify;

        private IPAddress URLToIP(string url)
        {
            try
            {
                IPAddress[] ips = Dns.GetHostEntry(url).AddressList;
                return ips[0];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return null;
            }
        }

        /// <summary>
        /// Gets a value indicating whether a Socket is connected to a remote host.
        /// </summary>
        /// <value>true if the socket is connected, otherwise false</value>>
        public bool IsConnected()
        {
            return socket.Connected;

        }

        
        /// <summary>
        /// Gets or sets the timeout value for sending and receiving. 
        /// If the the timeout value is exceeded an exception should be thrown
        /// </summary>
        /// <value>The timeout period in millisecs</value>
        public int TimeOut
        {
            set { socket.SendTimeout = socket.ReceiveTimeout = value; }
            get { return socket.SendTimeout; }

        }

        /// <summary>
        /// Opens the connection to the server.
        /// Pre:	The server can be reached by the url and portnumber
        /// Post:	Connection etablished
        /// </summary>
        /// <param name="url">Identification of the server. i.e a dns name or a ip address</param>
        /// <param name="port">Number between 1 and 65335, default 80</param>
        public void Open(string url, int port)
        {
            this.address = url;
            this.port = port;

            //IPEndPoint endpoint = new IPEndPoint(0, 0);
            //endpoint = (IPEndPoint)endpoint.Create(socketAddress);            
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        }

        //public SocketAddress socketAddress
        //{
        //    get;
        //    set;
        //}

        /// <summary>
        /// Closes the connection to server. 
        /// </summary>
        public void Close()
        {
            socket.Disconnect(true);
        }

        /// <summary>
        /// Sends a request to the server, i.e by using http
        /// Pre:	A connection is established to the server
        /// Post:	A request is sent to the server
        /// </summary>
        /// <param name="command">the request, i.e "GET /index.html"</param>
        public void SendRequest()
        {

            WebRequest requeste = HttpWebRequest.Create(uri);
            requeste.Timeout = TimeOut;
            requeste.GetResponse();

            return;
        }

        /// <summary>
        /// Receives the response from the server
        /// Pre:	A request has been sent to the server, and IsConnected is still true
        /// Post:	The response is returned in a string
        /// </summary>
        /// <returns>the response from the server</returns>
        public string Receive()
        {
            return null;
        }

        /// <summary>
        /// Gets a stream object to the server object, that might be used for common stream operations.
        /// Pre:	A connection is established to the server
        /// Post:	A stream object is returned, i.e a NetworkStream object.
        /// </summary>
        /// <returns>a stream object</returns>
        public Stream GetStream()
        {
            WebRequest request = HttpWebRequest.Create("http://www.noea.dk");
            request.Timeout = timeout;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader stream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII);

            return stream.BaseStream;
        }


    }
}

